#include "setformatdlg.h"
#include <QToolButton>
#include <QPushButton>
#include <QButtonGroup>
#include <QMenu>
#include <QPlainTextEdit>
#include <QHBoxLayout>
#include <QVBoxLayout>
#include <QColorDialog>
#include <QFontDialog>
#include <QFileDialog>
#include <QAction>
#include "common.h"
#include <QSpacerItem>
#include <QComboBox>
#include "myfiledialog.h"


extern QString str_properties;

SetFormatDlg::SetFormatDlg(Model model, QDialog *parent)
    :QDialog(parent)
    ,m_model(model)
{
    setFixedSize(600, 300);
    tool_btn_color = new QToolButton(this);
    tool_btn_color->setText(QObject::tr("add color"));


    QMenu *color_menu = new QMenu(this);

    color_menu->addAction(QObject::tr("color"));
    color_menu->addAction(QObject::tr("background-color"));
    color_menu->addAction(QObject::tr("alternate-background-color"));
    color_menu->addAction(QObject::tr("border-color"));
    color_menu->addAction(QObject::tr("border-top-color"));
    color_menu->addAction(QObject::tr("border-right-color"));
    color_menu->addAction(QObject::tr("border-bottom-color"));
    color_menu->addAction(QObject::tr("border-left-color"));
    color_menu->addAction(QObject::tr("gridline-color"));
    color_menu->addAction(QObject::tr("selection-color"));
    color_menu->addAction(QObject::tr("selection-background-color"));

    QAction *action_default = new QAction(QObject::tr("add color"), this);


    tool_btn_color->setPopupMode(QToolButton::MenuButtonPopup);//菜单弹出模式，执行按钮的Action
    tool_btn_color->setToolButtonStyle(Qt::ToolButtonTextBesideIcon); //按钮样式
    tool_btn_color->setDefaultAction(action_default);//关联Action
    tool_btn_color->setMenu(color_menu); //设置下拉菜单

    btn_add_resource = new QPushButton(QObject::tr("add resources"), this);
    btn_add_font = new QPushButton(QObject::tr("add font"), this);
    btn_cancel = new QPushButton(QObject::tr("cancel"), this);
    btn_ok = new QPushButton(QObject::tr("ok"), this);
    text_edit = new QPlainTextEdit(this);
    text_edit->setFixedSize(560, 200);
    QFont f = text_edit->font();
    f.setFamily("F:\\work\\dynamic_map_tool\\font\\simhei.ttf");
    f.setPixelSize(16);
    text_edit->setFont(f);

    box_timeformat = new QComboBox(this);

    QPixmap pixmap(1, 30);
    pixmap.fill(Qt::white);
    QIcon icon(pixmap);

    QStringList formatlist;
    formatlist<<"yyyy/MM/dd hh:mm"<<"yyyy-MM-dd hh:mm"<<"yyyyMMdd hh:mm";

    for (int i = 0; i <formatlist.size(); i++)
    {
        QString str = formatlist.at(i);
        box_timeformat->addItem(icon, str);
    }

    space = new QSpacerItem(btn_add_font->width(), btn_add_font->height(), QSizePolicy::Expanding,  QSizePolicy::Minimum);

    QHBoxLayout *layout_select = new QHBoxLayout;

    layout_select->addSpacing(20);
    layout_select->addWidget(btn_add_resource, 0, Qt::AlignLeft | Qt::AlignVCenter);
    layout_select->addWidget(tool_btn_color, 0, Qt::AlignLeft | Qt::AlignVCenter);
    layout_select->addWidget(btn_add_font, 0, Qt::AlignLeft | Qt::AlignVCenter);
    layout_select->addWidget(box_timeformat, 0, Qt::AlignLeft | Qt::AlignVCenter);
    layout_select->addSpacerItem(space);
    layout_select->setContentsMargins(0, 0, 0, 0);

     QHBoxLayout *layout_text = new QHBoxLayout;
     layout_text->addSpacing(20);
     layout_text->addWidget(text_edit, 0, Qt::AlignLeft | Qt::AlignVCenter);
     layout_text->addSpacing(20);
     layout_text->setContentsMargins(0, 0, 0, 0);

    QHBoxLayout *layout_btn = new QHBoxLayout;
    layout_btn->addStretch();
    layout_btn->addWidget(btn_cancel, 0, Qt::AlignLeft | Qt::AlignVCenter);
    layout_btn->addWidget(btn_ok, 0, Qt::AlignLeft | Qt::AlignVCenter);
    layout_btn->addSpacing(20);
    layout_btn->setContentsMargins(0, 0, 0, 0);

    QVBoxLayout *mainLayout = new QVBoxLayout;

    mainLayout->addLayout(layout_select);
    mainLayout->addLayout(layout_text);
    mainLayout->addLayout(layout_btn);
    mainLayout->setContentsMargins(0, 0, 0, 0);

    setLayout(mainLayout);
    setContentsMargins(0, 0, 0, 0);

    if (m_model == Resource)
    {
        btn_add_resource->setVisible(true);
        btn_add_font->setVisible(false);
        tool_btn_color->setVisible(false);
        box_timeformat->setVisible(false);
        disconnect(box_timeformat, static_cast<void(QComboBox::*)(const QString &)>(&QComboBox::currentIndexChanged), this, &SetFormatDlg::set_format);
    }
    else if (m_model == Style)
    {
       btn_add_font->setVisible(true);
       tool_btn_color->setVisible(true);
       btn_add_resource->setVisible(false);
       box_timeformat->setVisible(false);
       disconnect(box_timeformat, static_cast<void(QComboBox::*)(const QString &)>(&QComboBox::currentIndexChanged), this, &SetFormatDlg::set_format);

    }
    else if (m_model == TimeFormat)
    {
        btn_add_resource->setVisible(false);
        btn_add_font->setVisible(false);
        tool_btn_color->setVisible(false);
        box_timeformat->setVisible(true);
        connect(box_timeformat, static_cast<void(QComboBox::*)(const QString &)>(&QComboBox::currentIndexChanged), this, &SetFormatDlg::set_format);

    }
    else if (m_model == Text)
    {
        btn_add_resource->setVisible(false);
        btn_add_font->setVisible(false);
        tool_btn_color->setVisible(false);
        box_timeformat->setVisible(false);
        disconnect(box_timeformat, static_cast<void(QComboBox::*)(const QString &)>(&QComboBox::currentIndexChanged), this, &SetFormatDlg::set_format);
    }


    btn_group = new QButtonGroup(this);
    btn_group->addButton(btn_add_resource, 0);
    btn_group->addButton(btn_add_font, 1);
    btn_group->addButton(btn_cancel, 2);
    btn_group->addButton(btn_ok, 3);

//    connect(btn_group, QOverload<QAbstractButton *>::of(&QButtonGroup::buttonClicked), [=](QAbstractButton *btn)
//    {
//        if(btn == btn_add_resource)
//        {

////            QString dir = myapp::isDirExist(myapp::mDynamicMapParam->m_project_path) == true ? myapp::mDynamicMapParam->m_project_path : QDir::currentPath();
////            QString filter = QObject::tr("picture file") + "(*.jpg *.png *.gif);;" + QObject::tr("all file") + "(*.*);";
////            QString title = QObject::tr("please select a file");
////            QString path = QFileDialog::getOpenFileName(this, title, dir, filter);
////            QString path = QFileDialog::getExistingDirectory(this, title, dir, filter);
//            MyFileDialog file_dialog;
//            QStringList path;

//            if (file_dialog.exec() == QDialog::Accepted)
//            {
//                foreach (QString file, file_dialog.selectedFiles())
//                {
//                    QStringList list = file.split("/");
//                    path.append(list.last());
//                }
//            }

//            if (path.count() != 0)
//            {
//                for (int i = 0; i < path.count(); i++)
//                {
//                    text_edit->appendPlainText(path.at(i) + ";");
//                }
//            }
//        }
//        else if (btn == btn_add_font)
//        {
//            set_font();
//        }
//        else if(btn == btn_cancel)
//        {
//            reject();
//        }
//        else if (btn == btn_ok)
//        {
//            str_properties = text_edit->toPlainText();
//            accept();
//        }
//    });

    connect(tool_btn_color, &QToolButton::triggered, this, &SetFormatDlg::set_color);
}

void SetFormatDlg::set_format(const QString &format)
{
    text_edit->appendPlainText(format);
}

void SetFormatDlg::set_color(QAction *action)
{
    QString format_color;
    QColor color = QColorDialog::getColor();
    format_color = QString("%1rgb(%2, %3, %4);")
                          .arg(ZERO == action->text().compare("add color") ? "" : action->text() + ":")
                          .arg(color.red())
                          .arg(color.green())
                          .arg(color.blue());
    text_edit->appendPlainText(format_color);
}

void SetFormatDlg::set_font()
{
    bool ok = false;
    QFont f = QFontDialog::getFont(&ok);
    if (ok)
    {
        text_edit->appendPlainText(f.toString());
    }
}
